function [wage_OECD,omega,exports_US,exports_OECD,produced_US,produced_OECD,nontraded_US,nontraded_OECD,prices_US,prices_OECD,output_US,output_OECD,...
    markups_US,markups_OECD,mu_bar_US,mu_bar_OECD,GDP_US,GDP_OECD,export_share_US,export_share_OECD,revenue_US,revenue_OECD,x,m,...
    qualities_vec_consumed_US,qualities_vec_consumed_OECD,laborshare_US,laborshare_OECD,wage_level_US,wage_level_OECD,P_bar_US,P_bar_OECD,...
    total_income_US,total_income_OECD,real_consumption_US,real_consumption_OECD,real_product_wage_US,real_product_wage_OECD] ...
    = find_equilibrium(wage_guess_OECD,qualities_vec_US,qualities_vec_OECD,domestic_competitor_qual_vec_US,domestic_competitor_qual_vec_OECD,...
    import_tariff_US,import_tariff_OECD,L_US,L_OECD,n_varieties,export_share_US_guess,export_share_OECD_guess,mu_bar_US_guess,mu_bar_OECD_guess)

wage_US=1;
wage.guess_OECD = wage_guess_OECD;
wage_distance = 100;
mu_bar.step7_US = mu_bar_US_guess;
mu_bar.step7_OECD = mu_bar_OECD_guess;
export_share.step6_US = export_share_US_guess;
export_share.step6_OECD = export_share_OECD_guess;
counter_outer=0;
step7_distance=100;
mu_US_distance=100;
mu_OECD_distance=100;
mu_OECD_bounce_list=[];
mu_US_bounce_list=[];

% ITERATE STEPS 1-3 TO FIND WAGE THAT SATISFIES BALANCED TRADE
while (abs(step7_distance) > 1e-2 || abs(mu_US_distance)>1e-2 || abs(mu_OECD_distance)>1e-2) && counter_outer<=100
    counter_outer=counter_outer+1;
    counter=0;
    wage_bounce_list=[];

find_equilibrium_innerloop

% START WITH STEP 4 - GUESS MU_BAR USING EXPORT SHARE DATA
    
    mu_bar.step4_US = 1/((1 - export_share.step6_US) * (sum((1./markups_US(exports_US)) .* ...
                                                                      (1 / (sum(exports_US)+sum(nontraded_US)))) + ...
                                                          sum((1./markups_US(nontraded_US)) .* ...
                                                                      (1 / (sum(exports_US)+sum(nontraded_US))))) + ...
                                                          export_share.step6_US * (sum((1./markups_OECD(exports_US)).* ...
                                                                      (1 / (sum(exports_US))))));
    mu_bar.step4_OECD = 1/((1 - export_share.step6_OECD) * (sum((1./markups_OECD(exports_OECD)) .* ...
                                                                      (1 / (sum(exports_OECD)+sum(nontraded_OECD)))) + ...
                                                          sum((1./markups_OECD(nontraded_OECD)) .* ...
                                                                      (1 / (sum(exports_OECD)+sum(nontraded_OECD))))) + ...
                                                          export_share.step6_OECD * (sum((1./markups_US(exports_OECD)).* ...
                                                                      (1 / (sum(exports_OECD))))));
    
    %Step 5 -- calculate output
    output_US = nan(size(prices_US));
    output_US(produced_US) = ((wage_US * L_US * mu_bar.step4_US + (import_tariff_US-1) * export_share.step6_OECD * wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD) ...
        / n_varieties).*(1./prices_US(produced_US)); 
    output_US(exports_OECD) = ((wage_US * L_US * mu_bar.step4_US + (import_tariff_US-1) * export_share.step6_OECD * wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD) ...
        / n_varieties).*(1./prices_US(exports_OECD));
    
    output_OECD = nan(size(prices_US));
    output_OECD(produced_OECD) = ((wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD + (import_tariff_OECD-1) * export_share.step6_US * wage_US * L_US * mu_bar.step4_US) ...
        / n_varieties) .* (1./prices_OECD(produced_OECD));
    output_OECD(exports_US) = ((wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD + (import_tariff_OECD-1) * export_share.step6_US * wage_US * L_US * mu_bar.step4_US) ...
        / n_varieties) .* (1./prices_OECD(exports_US));
    
    %Step 6 
    GDP_US = sum(output_US(produced_US).*prices_US(produced_US)) + sum(output_OECD(exports_US).*prices_OECD(exports_US))/import_tariff_OECD;
    GDP_OECD = sum(output_OECD(produced_OECD).*prices_OECD(produced_OECD)) + sum(output_US(exports_OECD).*prices_US(exports_OECD))/import_tariff_US;
    
    %export share step6
    export_share.step6_US = (sum(output_OECD(exports_US).*prices_OECD(exports_US))/import_tariff_OECD)/GDP_US;
    export_share.step6_OECD = (sum(output_US(exports_OECD).*prices_US(exports_OECD))/import_tariff_US)/GDP_OECD;
    
    
    %Step 7 
    mu_bar.step4_US = 1/((1 - export_share.step6_US) * (sum((1./markups_US(exports_US)) .* ...
                                                                      (1 / (sum(exports_US)+sum(nontraded_US)))) + ...
                                                          sum((1./markups_US(nontraded_US)) .* ...
                                                                      (1 / (sum(exports_US)+sum(nontraded_US))))) + ...
                                                          export_share.step6_US * (sum((1./markups_OECD(exports_US)).* ...
                                                                      (1 / (sum(exports_US))))));
    mu_bar.step4_OECD = 1/((1 - export_share.step6_OECD) * (sum((1./markups_OECD(exports_OECD)) .* ...
                                                                      (1 / (sum(exports_OECD)+sum(nontraded_OECD)))) + ...
                                                          sum((1./markups_OECD(nontraded_OECD)) .* ...
                                                                      (1 / (sum(exports_OECD)+sum(nontraded_OECD))))) + ...
                                                          export_share.step6_OECD * (sum((1./markups_US(exports_OECD)).* ...
                                                                      (1 / (sum(exports_OECD))))));
                                                                  
                                                                  
    % NEED TO FIX THIS AS WELL TO REFLECT THE NEW BALANCED TRADE CONDITION                                                             
    %wage.step7_OECD = (mu_bar.step7_OECD*wage_US*L_US*m_guess)/(mu_bar.step7_US*L_OECD*x_guess);
    wage.step7_OECD = ((mu_bar.step7_US*wage_US*L_US*m_guess*import_tariff_OECD)/(x_guess*import_tariff_US)-...
        (import_tariff_OECD-1) * export_share.step6_US * mu_bar.step7_US * wage_US * L_US)...
        /(mu_bar.step7_OECD * L_OECD - (import_tariff_US-1) * (m_guess * import_tariff_OECD) / (x_guess * import_tariff_US) * export_share.step6_OECD * mu_bar.step7_OECD * L_OECD);
    
    % DISTANCE BETWEEN BALANCED TRADE WAGE AND MU_BAR GUESSES WAGE
    step7_distance = wage.step7_OECD - wage.balanced_trade_OECD;
    if counter_outer<5
         wage.guess_OECD = wage.balanced_trade_OECD + 0.05*step7_distance;
         mu_OECD_bounce_list = [mu_OECD_bounce_list; mu_bar.step4_OECD; mu_bar.step7_OECD];
         mu_US_bounce_list = [mu_US_bounce_list; mu_bar.step4_US; mu_bar.step7_US];
    else
         wage.guess_OECD = wage.balanced_trade_OECD + 0.01*step7_distance;
         mu_OECD_bounce_list = [mu_OECD_bounce_list; mu_bar.step4_OECD; mu_bar.step7_OECD];
         mu_US_bounce_list = [mu_US_bounce_list; mu_bar.step4_US; mu_bar.step7_US];
    end

    mu_US_distance = mu_bar.step7_US - mu_bar.step4_US;
    if counter_outer <= 5
         mu_bar.step7_US = 0.5*mu_bar.step4_US+0.5*mu_bar.step7_US;       
    elseif counter_outer>5 && counter_outer <= 10
         mu_bar.step7_US = 0.1*mu_bar.step4_US+0.9*mu_bar.step7_US;
    elseif counter_outer > 10
         mu_bar.step7_US = mean(mu_US_bounce_list(end-3:end));
    end

%     if counter < 6
%          mu_bar.step7_US = 0.5*mu_bar.step4_US+0.5*mu_bar.step7_US;       
%     else
%          mu_bar.step7_US = 0.1*mu_bar.step4_US+0.9*mu_bar.step7_US;
%     end
    
    mu_OECD_distance = mu_bar.step7_OECD - mu_bar.step4_OECD;
    if counter_outer <= 5
         mu_bar.step7_OECD = 0.5*mu_bar.step4_OECD+0.5*mu_bar.step7_OECD;
    elseif counter_outer>5 && counter_outer <= 10
         mu_bar.step7_OECD = 0.1*mu_bar.step4_OECD+0.9*mu_bar.step7_OECD;
    elseif counter_outer > 10
         mu_bar.step7_OECD = mean(mu_OECD_bounce_list(end-3:end));
    end

%     if counter < 6
%          mu_bar.step7_OECD = 0.5*mu_bar.step4_OECD+0.5*mu_bar.step7_OECD;       
%     else
%          mu_bar.step7_OECD = 0.1*mu_bar.step4_OECD+0.9*mu_bar.step7_OECD;
%     end

    wage_distance = step7_distance;

%     if counter>=990
%         disp('ugh')
%     end

end

    % IF CONVERGENCE DOES NOT OCCUR IN 150 LOOPS USE THE AVERAGES
 
% TAKE ALL THE FINAL VARIABLE VALUES AND NAME THEM OUTPUT FROM THIS FUNCTION
if counter_outer>=100;
    %disp('DID NOT CONVERGE')u
    mu_bar_US = mean(mu_US_bounce_list(end-99:end));
    mu_bar_OECD = mean(mu_OECD_bounce_list(end-99:end));
    find_equilibrium_innerloop
    wage_OECD = wage.balanced_trade_OECD;
    
    %Calculate output using averaged mu's from bounce list
    output_US = nan(size(prices_US));
    output_US(produced_US) = ((wage_US * L_US * mu_bar.step4_US + (import_tariff_US-1) * export_share.step6_OECD * wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD) ...
        / n_varieties).*(1./prices_US(produced_US)); 
    output_US(exports_OECD) = ((wage_US * L_US * mu_bar.step4_US + (import_tariff_US-1) * export_share.step6_OECD * wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD) ...
        / n_varieties).*(1./prices_US(exports_OECD));
    
    output_OECD = nan(size(prices_US));
    output_OECD(produced_OECD) = ((wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD + (import_tariff_OECD-1) * export_share.step6_US * wage_US * L_US * mu_bar.step4_US) ...
        / n_varieties) .* (1./prices_OECD(produced_OECD));
    output_OECD(exports_US) = ((wage.balanced_trade_OECD * L_OECD * mu_bar.step4_OECD + (import_tariff_OECD-1) * export_share.step6_US * wage_US * L_US * mu_bar.step4_US) ...
        / n_varieties) .* (1./prices_OECD(exports_US));
    
    %Step 6 
    GDP_US = sum(output_US(produced_US).*prices_US(produced_US)) + sum(output_OECD(exports_US).*prices_OECD(exports_US))/import_tariff_OECD;
    GDP_OECD = sum(output_OECD(produced_OECD).*prices_OECD(produced_OECD)) + sum(output_US(exports_OECD).*prices_US(exports_OECD))/import_tariff_US;
    
    %export share step6
    export_share_US = (sum(output_OECD(exports_US).*prices_OECD(exports_US))/import_tariff_OECD)/GDP_US;
    export_share_OECD = (sum(output_US(exports_OECD).*prices_US(exports_OECD))/import_tariff_US)/GDP_OECD;
    
else
    
wage_OECD = wage.balanced_trade_OECD;
export_share_US=export_share.step6_US;
export_share_OECD=export_share.step6_OECD;
mu_bar_US = mu_bar.step7_US;
mu_bar_OECD = mu_bar.step7_OECD;
end

revenue_US = prices_US.*output_US;
revenue_OECD = prices_OECD.*output_OECD;

x = sum(exports_US) / length(exports_US);
m = sum(exports_OECD) / length(exports_OECD);

qualities_vec_consumed_US=qualities_vec_US;
qualities_vec_consumed_US(exports_OECD)=qualities_vec_OECD(exports_OECD);
    
qualities_vec_consumed_OECD=qualities_vec_OECD;
qualities_vec_consumed_OECD(exports_US)=qualities_vec_US(exports_US);

wage_level_US = geomean(abs(real([qualities_vec_US(produced_US)./markups_US(produced_US); ...
    (qualities_vec_OECD(exports_OECD)./markups_US(exports_OECD))*(wage_US/(wage_OECD*import_tariff_US))])));

wage_level_OECD = geomean(abs(real([qualities_vec_OECD(produced_OECD)./markups_OECD(produced_OECD); ...
    (qualities_vec_US(exports_US)./markups_OECD(exports_US))*(wage_OECD/(wage_US*import_tariff_OECD))])));

real_product_wage_US = prod((qualities_vec_US(produced_US)./markups_US(produced_US)).^(revenue_US(produced_US)./GDP_US)) ...
    * prod((qualities_vec_US(exports_US)./(markups_OECD(exports_US)*import_tariff_OECD)).^(revenue_OECD(exports_US)./(import_tariff_OECD*GDP_US)));

real_product_wage_OECD = prod((qualities_vec_OECD(produced_OECD)./markups_OECD(produced_OECD)).^(revenue_OECD(produced_OECD)./GDP_OECD)) ...
    * prod((qualities_vec_OECD(exports_OECD)./(markups_US(exports_OECD)*import_tariff_US)).^(revenue_US(exports_OECD)./(import_tariff_US*GDP_OECD)));

P_bar_US = geomean([prices_US(produced_US)./qualities_vec_US(produced_US);prices_US(exports_OECD)./qualities_vec_OECD(exports_OECD)]);
P_bar_OECD = geomean([prices_OECD(produced_OECD)./qualities_vec_OECD(produced_OECD);prices_OECD(exports_US)./qualities_vec_US(exports_US)]);

laborshare_US = 1/mu_bar_US;
laborshare_OECD = 1/mu_bar_OECD;

total_income_US = wage_US * L_US * mu_bar_US + (import_tariff_US-1) * export_share_OECD * wage_OECD * L_OECD * mu_bar_OECD;
total_income_OECD = wage_OECD * L_OECD * mu_bar_OECD + (import_tariff_OECD-1) * export_share_US * wage_US * L_US * mu_bar_US;

real_consumption_US = (total_income_US/P_bar_US)/L_US;
real_consumption_OECD = (total_income_OECD/P_bar_OECD)/L_US;

end


